<?php

namespace App\Services\Member;

use App\Models\Member\FreezeMoneyLog;
use App\Models\Member\Member;
use App\Models\Order\OrdersFreezes;
use Illuminate\Support\Facades\DB;

class FreezeMoneyService
{
    /**
     * 冻结金收入
     * @param string $member_uuid  用户uuid
     * @param float $cash  单位元
     * @param int $source_type  数据来源类型 1广告收入 2打赏收入 3活动收入 11广告释放支出 12打赏释放支出 13活动释放支出
     * @param int $source_uuid  数据uuid
     * @param string $content  具体变动内容说明
     * @param string $img  显示图片
     * @return bool
     * @throws \Exception
     */
    public static function income(array $order_ids, string $member_uuid, float $cash, int $source_type, string $content='', $img='', string $source_uuid=''){
        try {
            if($cash <= 0) return true;

            DB::beginTransaction();

            $member = Member::where('uuid', $member_uuid)->first();
            $before_freeze_money = $member->freeze_money;

            //变动用户金额
            $after_freeze_money = bcadd($member->freeze_money, $cash, 2);
            $member->update([
                'freeze_money' => $after_freeze_money
            ]);

            //日志
            $freezeMoneyLog = FreezeMoneyLog::create([
                'member_uuid' => $member_uuid,
                'content' => $content,
                'before_freeze_money' => $before_freeze_money,
                'after_freeze_money' => $after_freeze_money,
                'cash' => $cash,
                'type' => 1,
                'month' => date('Y-m-01'),
                'source_type' => $source_type,
                'source_uuid' => $source_uuid,
                'state' => 0,
                'img' => $img,
            ]);

            foreach ($order_ids as $order_id){
                OrdersFreezes::create([
                    'freeze_money_log_id' => $freezeMoneyLog->id,
                    'order_id' => $order_id,
                ]);
            }

            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();

            throw new \Exception($e->getMessage().$e->getLine());
        }

        return true;
    }

    /**
     * 冻结金支出
     * @param string $member_uuid  用户uuid
     * @param float $cash  单位元
     * @param int $source_type  数据来源类型 1广告收入 2打赏收入 3活动收入 11广告释放支出 12打赏释放支出 13活动释放支出
     * @param int $source_uuid  数据uuid
     * @param string $content  具体变动内容说明
     * @param string $img  显示图片
     * @return bool
     * @throws \Exception
     */
    public static function out(array $order_ids, string $member_uuid, float $cash, int $source_type, string $content='', string $img='', string $source_uuid=''){
        try {
            DB::beginTransaction();
            $member = Member::where('uuid', $member_uuid)->first();
            $before_freeze_money = $member->freeze_money;

            //变动用户冻结金
            $after_freeze_money = bcsub($member->freeze_money, $cash, 2);
            $member->update([
                'freeze_money' => $after_freeze_money
            ]);

            //冻结金日志
            $freezeMoneyLog = FreezeMoneyLog::create([
                'member_uuid' => $member_uuid,
                'content' => $content,
                'before_freeze_money' => $before_freeze_money,
                'after_freeze_money' => $after_freeze_money,
                'cash' => $cash,
                'type' => 2,
                'month' => date('Y-m-01'),
                'source_type' => $source_type,
                'source_uuid' => $source_uuid,
                'state' => 1,
                'img' => $img,
            ]);

            foreach ($order_ids as $order_id){
                OrdersFreezes::create([
                    'freeze_money_log_id' => $freezeMoneyLog->id,
                    'order_id' => $order_id,
                ]);
            }

            //增加用户可用余额收入
            switch ($source_type){
                case 11:
                    MoneyService::income($order_ids, $member_uuid, $cash, 3, $content, $img);
                    break;
                case 12:
                    MoneyService::income($order_ids, $member_uuid, $cash, 4, $content, $img);
                    break;
                case 13:
                    MoneyService::income($order_ids, $member_uuid, $cash, 6, $content, $img);
                    break;

            }

            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();

            throw new \Exception($e->getMessage().$e->getLine());
        }

        return true;

    }

}
